package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/mobile/validateCode")
public class MobileController {

    @Autowired
    private RedisTemplate redisTemplate;
     /**
     *   C端手机号获取短信验证码
     * @param type
     * @param telephone
     * @param
     */
     @PostMapping("/send")    //13412345678
    public Result send(String type, String  telephone ) {

             // 使用工具类产生6位数的验证码
              String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
             log.info("[手机号,类型,验证码]" + telephone + "==="+type+"===="+verifyCode);

             //  以类型和手机号连接作为key
             String redisKey = type + ":" + telephone;
             log.info("redisKey,value"+redisKey,verifyCode);

            // 使用redis存储
         redisTemplate.opsForValue().set(redisKey,verifyCode,5,TimeUnit.MINUTES);

           return  new Result(true,MessageConst.TELEPHONE_VALIDATECODE_NOTNULL);

     }

}
